package cn.devices.mapper;


import cn.devices.dto.DeviceDto;
import cn.devices.dto.TaskDto;
import cn.devices.entity.TbTask;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 设备盘点任务表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbTaskMapper {

    @Insert("insert into tb_task(task_id, start_time, " +
            " task_status,disabled) " +
            "VALUE (#{taskId},#{startTime}," +
            "#{taskStatus},0)")
    int taskAdd(TbTask tbTask);

    @Update("update tb_task set end_time=#{endTime} ,in_stock=#{inStock}," +
            "out_stock=#{outStock},warn_stock=#{warnStock},task_spec=#{taskSpec},task_status=#{taskStatus}\n" +
            "where task_id=#{taskId}")
    int taskUpdate(TbTask tbTask);

    @Update("update tb_task set disabled=1 where task_id=#{taskId}")
    int taskDelete(String taskId);

    /**
     *
     * @return
     */
    @Select("select count(*)                totalCount,\n" +
            "       (select count(*)\n" +
            "        from tb_device\n" +
            "        where is_borrow = '1') borrowCount\n" +
            "         from tb_device")
    TaskDto taskDto();

    @Select("select count(*) realSurCount,(select count(*) from tb_inventory where device_status='2') malfunctionCount\n" +
            "from tb_inventory\n" +
            "where task_id = #{taskId}")
    TaskDto taskDto2(String taskId);

    @Select("select *\n" +
            "from tb_task where disabled=0 order by start_time desc")
    List<TbTask> tbTaskList();

    @Select("select *\n" +
            "from tb_task where task_id=#{taskId}")
    TbTask tbTaskById(String taskId);
    
    @Select("select * from tb_task where task_status='1'")
    TbTask tbTaskProcess();


    //未在库
    @Select("select *\n" +
            "from tb_device\n" +
            "where is_borrow = '0'\n" +
            "  and device_code not in (select tb_inventory.device_code\n" +
            "                      from tb_inventory\n" +
            "                               inner join tb_task tt on tb_inventory.task_id = tt.task_id\n" +
            "                   where tt.task_id=#{taskId} )")
    List<DeviceDto> deviceDtoList1(String taskId);


    //故障
    @Select("select td.*,tb_inventory.inventory_info message\n" +
            "from tb_inventory\n" +
            "         inner join tb_device td on tb_inventory.device_code = td.device_code\n" +
            "where task_id=#{taskId} and priority!='3'\n" +
            "order by priority")
    List<DeviceDto> deviceDtoList2(String taskId);

    //故障
    @Select("select td.*,tb_inventory.inventory_info message\n" +
            "from tb_inventory\n" +
            "         inner join tb_device td on tb_inventory.device_code = td.device_code\n" +
            "where task_id=#{taskId}\n" +
            "order by priority")
    List<DeviceDto> deviceDtoList3(String taskId);




}
